<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LogController extends Controller
{
    //加载登录模板
    public function loginFrom(){
        return view('admin.log.login');
    }
    //处理登录业务
    public function login(Request $request){
//        dd($request->toArray());
        $request->validate([
            'account'=>'required',
            'password'=>'required'
        ],[
            'account.required'=>'账号不能为空',
            'password.required'=>'密码不能为空',
        ]);
//        dd($request->toArray());
        if (filter_var($request->account,FILTER_VALIDATE_EMAIL)){
           $data['email']=$request->account;
        }else{
            $data['phone']=$request->account;
        }
        $data['password']=$request->password;
//        dd($data);
       $res= auth('admin')->attempt($data);
//       dd($res);
            if ($res){
                return redirect()->route('admin.index.index')->with('success','登录成功');
            }else{
                return back()->with('error','登录失败，重新登录');
            }
    }
    //退出登录
    public function logout(){
        auth('admin')->logout();
        return redirect()->route('admin.log.loginFrom')->with('success','退出成功');
    }
}
